package com.binvshe.binvshe.literature;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.release.ContentType;
import com.pachong.android.baseuicomponent.activity.SwipeListActivity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.frameworkbase.utils.EasyToast;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Simple to Introduction
 *
 * @ProjectName: net.horien.mall.community
 * @Description: [一句话描述该类的功能]
 * @Author: Administrator
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2017/1/3 15:38
 * @Version: 1.0.0
 */
public class LiteratureManagementActivity extends SwipeListActivity {

    public static void start(Context context) {
        Intent intent = new Intent(context, LiteratureManagementActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("文学管理");
        enableBackButton();
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        reload();
    }

    void reload() {
        mPage = 0;
        getAdapter().getData().clear();
        getAdapter().notifyDataSetChanged();
        mRequestListener = new ListDataRequestListener(this, ArticalBean.class);
        startLoading();
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new TemplateListAdapter(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        reload();
    }

    int mPage;
    ListDataRequestListener mRequestListener = new ListDataRequestListener(this, ArticalBean.class);

    @Override
    public void onStartLoading() {
        if (mPage == 0 || mRequestListener.getResponseDataParser().hasMore()) {
            mPage++;
            IHttpRequest request = new MyHttpRequest(this);
            UrlParams params = new UrlParams();
            params.put("page", mPage + "");
            params.put("size", 8 + "");
            request.get(UrlCenter.ARTICAL_LIST, params, mRequestListener);
        }
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        ArticalBean bean = (ArticalBean) getData().get(position);
        Intent intent = new Intent();
        intent.putExtra("type", bean);
        if (TextUtils.equals(ContentType.LONG_STORY, bean.getMedia_type())) {
            // 连载
            LiteratureChapterListActivity.start(this, LiteratureChapterListActivity.TYPE_SERIAL, bean);
        } else {
            // 单篇
            LiteratureChapterListActivity.start(this, LiteratureChapterListActivity.TYPE_SINGLE, bean);

        }
    }

    /**
     * Adapter
     */
    public class TemplateListAdapter extends RecyclerViewBaseAdapter<ArticalBean> {

        public TemplateListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup viewGroup, int i) {
            return new ItemViewHolder(setAdapterViewItem(viewGroup, R.layout.list_item_literature_management));
        }


        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
            final ItemViewHolder holder = (ItemViewHolder) viewHolder;
            final ArticalBean bean = getData().get(i);
            holder.btnManage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (TextUtils.equals(ContentType.LONG_STORY, bean.getMedia_type())) {
                        // 连载
                        LiteratureChapterListActivity.start(LiteratureManagementActivity.this, LiteratureChapterListActivity.TYPE_SERIAL, bean);
                    } else {
                        // 单篇
                        LiteratureChapterListActivity.start(LiteratureManagementActivity.this, LiteratureChapterListActivity.TYPE_SINGLE, bean);
                    }
                }
            });

            holder.btnFinish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (TextUtils.equals(ContentType.LONG_STORY, bean.getMedia_type())) {
                        // 连载
                    } else {
                        // 单篇
                    }
                    reqFinishArtical(""+bean.getId(), holder.btnFinish);
                }
            });


            if (TextUtils.equals(ContentType.LONG_STORY, bean.getMedia_type())) {
                holder.tvTag.setText("连载");
                holder.tvTag.setBackgroundResource(R.drawable.btn_literature_serial);

            } else {
                holder.tvTag.setText("单篇");
                holder.tvTag.setBackgroundResource(R.drawable.btn_literature_single);

            }

            holder.tvTitle.setText(bean.getTitle());

            if (TextUtils.equals(ContentType.SHORT_STORY, bean.getMedia_type())
                    || bean.getFlag() == 1) {
                holder.btnFinish.setEnabled(false);
            } else {
                holder.btnFinish.setEnabled(true);
            }
        }

        class ItemViewHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.tvTag)
            TextView tvTag;
            @Bind(R.id.tvTitle)
            TextView tvTitle;
            @Bind(R.id.btnManage)
            Button btnManage;
            @Bind(R.id.btnFinish)
            Button btnFinish;

            ItemViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

        }

    }

    private void reqFinishArtical(String idStr, final Button btnFinish) {
        IHttpRequest request = new MyHttpRequest(this);
        String url = String.format(UrlCenter.ARTICAL_END, idStr);
        request.put(url, null, new DataRequestListener<String>(String.class) {

            @Override
            public void onSuccess(String acct) {
                super.onSuccess(acct);
                btnFinish.setEnabled(false);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                EasyToast.showToast(LiteratureManagementActivity.this, strMsg);
            }
        });
    }


}